package baseFx.tests.common.caching.redis;

import baseFx.common.Func1T;
import baseFx.common.caching.redis.RedisListCache;
import baseFx.common.collections.RemoteList;
import baseFx.common.concurrent.Task;
import baseFx.serialization.json.JsonUtils;
import redis.clients.jedis.Jedis;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class TestRedisListCache {
    public static void main(String[] args) {
        test_4();
    }

    //普通使用
    private static void test_1() {
        Jedis jedis = new Jedis();
        jedis.auth(Config.redis_password);
        RemoteList<Model> cache = new RedisListCache<>(jedis, Model.class, UUID.randomUUID().toString(), 2);

        Model m = new Model();
        m.id = 1;
        m.name = "aaa";
        m.time = new Date();
        cache.add(m);
        m.id = 2;
        m.name = "bbb";
        m.time = new Date(System.currentTimeMillis() + 1000);
        cache.add(m);
        m.id = 3;
        m.name = "ccc";
        m.time = new Date(System.currentTimeMillis() + 2000);
        cache.add(m);
        System.out.println(JsonUtils.toJson(cache.get(0)));
        System.out.println(JsonUtils.toJson(cache.get(1)));
        System.out.println(JsonUtils.toJson(cache.get(2)));
        Task.delay(2000);
        System.out.println(cache.size());
    }

    //指定位置add
    private static void test_2() {
        Jedis jedis = new Jedis();
        jedis.auth(Config.redis_password);
        RemoteList<Integer> cache = new RedisListCache<>(jedis, Integer.class, UUID.randomUUID().toString(), 10);

        cache.add(1);
        cache.add(3);
        cache.add(1, 2);
        List<Integer> items = Arrays.asList(5, 6, 7);
        cache.addAll(1, items);
        for (Integer item : cache) {
            System.out.println(item);
        }
    }

    //subList方法
    private static void test_3() {
        Jedis jedis = new Jedis();
        jedis.auth(Config.redis_password);
        RemoteList<Integer> cache = new RedisListCache<>(jedis, Integer.class, UUID.randomUUID().toString(), 10);

        cache.add(1);
        cache.add(2);
        cache.add(3);
        cache.add(4);
        cache.add(5);
        List<Integer> items = cache.subList(1, 3);
        for (Integer item : items) {
            System.out.println(item);
        }
    }

    //查找元素
    private static void test_4() {
        Jedis jedis = new Jedis();
        jedis.auth(Config.redis_password);
        RemoteList<Integer> cache = new RedisListCache<>(jedis, Integer.class, UUID.randomUUID().toString(), 10);
        cache.addAll(Arrays.asList(1, 2, 3, 4, 5));
        System.out.println(cache.indexOf((Func1T<Integer, Boolean>) i -> i == 3));
        System.out.println(cache.lastIndexOf((Func1T<Integer, Boolean>) i -> i == 2));
    }

    //删除元素
    private static void test_5() {
        Jedis jedis = new Jedis();
        jedis.auth(Config.redis_password);
        RemoteList<Integer> cache = new RedisListCache<>(jedis, Integer.class, UUID.randomUUID().toString(), 10);
        cache.addAll(Arrays.asList(1, 2, 3, 4, 5));
        cache.remove((Object) 2);
        cache.remove(2);
        for (Integer item : cache) {
            System.out.println(item);
        }
    }

    private static class Model {
        public Integer id;
        public String name;
        public Date time;
    }
}
